package com.example.news;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.dao.UserDao;
import com.example.news.entity.Userinfo;

import java.util.Objects;

/**
 * @author Admin
 */
public class ChangePwdActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_user_name;
    private EditText et_user_pwd;
    private EditText et_user_pwd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        tv_user_name = findViewById(R.id.tv_user_name);
        et_user_pwd = findViewById(R.id.et_user_pwd);
        et_user_pwd2 = findViewById(R.id.et_user_pwd2);
        findViewById(R.id.btn_confrim).setOnClickListener(this);

        Userinfo userinfo = Userinfo.getUserinfo();
        if (Objects.nonNull(userinfo)) {
            tv_user_name.setText(userinfo.getUserName());
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_confrim) {
            String newPwd = et_user_pwd.getText().toString();
            String newPwd2 = et_user_pwd2.getText().toString();
            if (TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(newPwd2)) {
                Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            String userName = tv_user_name.getText().toString();
            boolean b = UserDao.changeUpdate(userName, newPwd);
            if (b) {
                Userinfo.setUserinfo(null);
                Toast.makeText(this, "修改成功 请登陆", Toast.LENGTH_SHORT).show();
                finish();
//                Intent intent = new Intent(this, LoginActivity.class);
//                startActivity(intent);
            }
        }
    }
}